<?php
	session_start();
	include_once("conn.php");
	
	
	if(!isset($_SESSION["current_user"]))
	{
		echo "<script>alert('请先登录');history.back();</script>";
		exit;
	}

else
{
	$reply_user = $_SESSION["current_user"];				//回帖者，即当前登录者

	date_default_timezone_set("PRC");
	$reply_time = date("Y-m-d H:i:s",time());			//回帖时间
	$r_week = date("D");			//回帖星期几
	//插入回帖需要：回帖作者（当前登录者）、回帖内容、回的主题ID、回的版面ID，名、回帖时间，(楼主ID？)
	
	$reply_content = $_POST["reply_content"];				//回帖内容
	$reply_tid = $_POST["r_tid"];						//回的主题的ID
	$reply_s_name = $_POST["r_board_name"];							//回帖所在的版面，这个好象没用？
	$reply_t_name = $_POST["r_t_name"];					//回的主题的题目

	$sql = "select * from bbs_session where s_name = '".$reply_s_name."'";
	$result = mysql_query($sql);
	$q_result = mysql_fetch_array($result);
	$reply_s_id = $q_result["s_id"];					//回的版面ID
	
	$sql = "select * from bbs_user where u_name = '".$reply_user."'";
	$result = mysql_query($sql);
	$q_result = mysql_fetch_array($result);
	$reply_u_id = $q_result["u_id"];						//回帖者ID

	$sql = "insert into bbs_reply(r_tid,r_sid,r_uid,r_content,r_time,r_u_name,r_t_name,r_s_name,r_week) values('".$reply_tid."','".$reply_s_id."','".$reply_u_id."','".$reply_content."','".$reply_time."','".$reply_user."','".$reply_t_name."','".$reply_s_name."','".$r_week."')";
	$result = mysql_query($sql);
	if($result)
	{
		$sql2 = "update bbs_topic set t_last_reply_name = '".$reply_user."' where t_id = '".$reply_tid."'";
		$sql3 = "update bbs_topic set t_last_reply_time = '".$reply_time."' where t_id = '".$reply_tid."'";
		$result2 = mysql_query($sql2);
		$result3 = mysql_query($sql3);
		if($result2 && $result3)
		{
		
			echo "<script>history.back();</script>";
		}
		else {
			echo "<script>alert('回复失败，更新最后发帖者，时间出错');</script>";
		}
	}
	else {
		echo "<script>alert('回复失败');history.back();</script>";
	}
}
?>
<html>
<head>
<meta charset= "utf-8">
</head>
</html>